Skip to content

Conversation

markbhasawut
Copy link
Contributor

This PR updates the PMADDWD/PMADDUBSW builtins to support constant expression handling, by extending the VectorExprEvaluator::VisitCallExpr that handles interp__builtin_ia32_pmadd builtins.

Closes #155392

Copy link

github-actions bot commented Oct 1, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@markbhasawut markbhasawut changed the title [Headers][X86] Enable constexpr handling for MMX/SSE/AVX/AVX512 PMADDWD/PMADDUBSW intrinsics intrinsics [Headers][X86] Enable constexpr handling for MMX/SSE/AVX/AVX512 PMADDWD/PMADDUBSW intrinsics Oct 1, 2025
@RKSimon RKSimon self-requested a review October 6, 2025 13:29
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage in sse2-builtins.c etc.

case clang::X86::BI__builtin_ia32_pmaddubsw128:
case clang::X86::BI__builtin_ia32_pmaddubsw256:
case clang::X86::BI__builtin_ia32_pmaddubsw512:
return interp__builtin_ia32_pmadd(S, OpPC, Call, BuiltinID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a callback system like interp__builtin_elementwise_int_binop:

return interp__builtin_ia32_pmadd(S, OpPC, Call, 
  [](const APSInt &LoLHS, const APSInt &HiLHS, const APSInt &HiRHS, const APSInt &HiRHS) {
    unsigned BitWidth = 2 * LHS.getBitWidth();
    return (LoLHS.zext(BitWidth) * LoRHS.sext(BitWidth)).sadd_sat((HiLHS.zext(BitWidth) * HiRHS.sext(BitWidth)));
  });

case clang::X86::BI__builtin_ia32_pmaddwd128:
case clang::X86::BI__builtin_ia32_pmaddwd256:
case clang::X86::BI__builtin_ia32_pmaddwd512:
return interp__builtin_ia32_pmadd(S, OpPC, Call, BuiltinID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a callback system like interp__builtin_elementwise_int_binop:

return interp__builtin_ia32_pmadd(S, OpPC, Call, 
  [](const APSInt &LoLHS, const APSInt &HiLHS, const APSInt &HiRHS, const APSInt &HiRHS) {
    unsigned BitWidth = 2 * LHS.getBitWidth();
    return (LoLHS.sext(BitWidth) * LoRHS.sext(BitWidth)) + (HiLHS.sext(BitWidth) * HiRHS.sext(BitWidth));
  });

case clang::X86::BI__builtin_ia32_pmaddubsw512:
Mul0 = APSInt(U_LHS0.zext(BitWidth) * RHS0.sext(BitWidth));
Mul1 = APSInt(U_LHS1.zext(BitWidth) * RHS1.sext(BitWidth));
Result = APSInt(Mul0.sadd_sat(Mul1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing signedness control:

bool DestUnsigned = Call->getType()->isUnsignedIntegerOrEnumerationType();
Result = APSInt(..., DestUnsigned);

case clang::X86::BI__builtin_ia32_pmaddwd512:
Mul0 = APSInt(LHS0.sext(BitWidth) * RHS0.sext(BitWidth));
Mul1 = APSInt(LHS1.sext(BitWidth) * RHS1.sext(BitWidth));
Result = APSInt(Mul0 + Mul1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing signedness control

case clang::X86::BI__builtin_ia32_pmaddwd512:
ResultElements.push_back(
APValue(APSInt(LHS0.sext(BitWidth) * RHS0.sext(BitWidth) +
LHS1.sext(BitWidth) * RHS1.sext(BitWidth))));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing signedness control

ResultElements.push_back(APValue(
APSInt(APInt(U_LHS0.zext(BitWidth)) *
RHS0.sext(BitWidth).sadd_sat(APInt(U_LHS1.zext(BitWidth)) *
RHS1.sext(BitWidth)))));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing signedness control

@markbhasawut
Copy link
Contributor Author

Missing test coverage in sse2-builtins.c etc.

I'm working on it. Sorry for taking such a long time on the issue. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - add MMX/SSE/AVX/AVX512 PMADDWD/PMADDUBSW intrinsics to be used in constexpr
2 participants